package com.snow.fvmusic.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snow.fvmusic.bo.ApiException;
import com.snow.fvmusic.bo.ResultCode;
import com.snow.fvmusic.generator.entities.User;
import com.snow.fvmusic.generator.entities.UserConcernRelation;
import com.snow.fvmusic.generator.mapper.UserConcernRelationMapper;
import com.snow.fvmusic.generator.mapper.UserMapper;
import com.snow.fvmusic.service.ConcernOrFansService;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author ：snow
 * @Date ：Created in 2020-11-10
 * @Description：
 * @Modified By：
 * @Version:
 */
@Service
public class ConcernOrFansServiceImpl implements ConcernOrFansService {
    @Resource
    private UserConcernRelationMapper userConcernRelationMapper;
    @Resource
    private UserMapper userMapper;
    @Transactional
    @Override
    public int concern(Long concernUserId, Long userId) {
        UserConcernRelation re = new UserConcernRelation();
        re.setConcernUserId(concernUserId);
        re.setUserId(userId);
        int insert = 0;
        try{
            insert = userConcernRelationMapper.insert(re);
            userMapper.incrOrDecrConcernCount(userId, 1);
            userMapper.incrOrDecrFansCount(concernUserId, 1);
        }catch (Exception e){
            throw new ApiException(ResultCode.ILLEGAL, "不可重复关注");
        }
        return insert;
    }

    @Transactional
    @Override
    public int cancelConcern(Long concernUserId, Long userId) {
        QueryWrapper<UserConcernRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("concern_user_id", concernUserId);
        wrapper.eq("user_id", userId);
        int delete = userConcernRelationMapper.delete(wrapper);
        if (delete == 1){
            userMapper.incrOrDecrConcernCount(userId, -1);
            userMapper.incrOrDecrFansCount(concernUserId, -1);
        }
        return delete;
    }

    @Override
    public Page<User> getUserFans(Long userId, Integer page, Integer size) {
        QueryWrapper<UserConcernRelation> relaWrapper = new QueryWrapper<>();
        relaWrapper.eq("concern_user_id", userId);
        relaWrapper.select("user_id");
        List<UserConcernRelation> relations = userConcernRelationMapper.selectList(relaWrapper);
        List<Long> fansIds = relations.stream().map(UserConcernRelation::getUserId).collect(Collectors.toList());
        Page<User> userPage = new Page<>(page, size);
        if (!CollUtil.isEmpty(fansIds)){
            QueryWrapper<User> userWrapper = new QueryWrapper<>();
            userWrapper.select("id", "username","avatar_url");
            userWrapper.in("id", fansIds);
            userPage = userMapper.selectPage(userPage, userWrapper);

        }
        return userPage;
    }

    @Override
    public Page<User> getUserConcerns(Long userId, Integer page, Integer size) {
        Page<User> userPage = new Page<>(page, size);
        QueryWrapper<UserConcernRelation> relationWrapper = new QueryWrapper<>();
        relationWrapper.eq("user_id", userId);
        relationWrapper.select("concern_user_id");
        List<UserConcernRelation> relations = userConcernRelationMapper.selectList(relationWrapper);
        List<Long> concIds = relations.stream().map(UserConcernRelation::getConcernUserId).collect(Collectors.toList());

        if (!CollUtil.isEmpty(concIds)){
            QueryWrapper<User> userWrapper = new QueryWrapper<>();
            userWrapper.select("username", "id", "avatar_url");
            userWrapper.in("id", concIds);
            userPage = userMapper.selectPage(userPage, userWrapper);
        }
        return userPage;
    }
}
